home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / 2004-12 CHIP.iso / CHIP / Porady / Srodowisko PHP-MySQL / ACTIVESTATE PERL ADD-ON / PERL_add-on.exe / {app} / perl / bin / splain.bat < prev    next >
DOS Batch File  |  2004-08-02  |  17KB  |  633 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl
  14. #line 15
  15.     eval 'exec c:\wamp\perl\\bin\perl.exe -S $0 ${1+"$@"}'
  16.     if $running_under_some_shell;
  17.  
  18. =head1 NAME
  19.  
  20. diagnostics, splain - produce verbose warning diagnostics
  21.  
  22. =head1 SYNOPSIS
  23.  
  24. Using the C<diagnostics> pragma:
  25.  
  26.     use diagnostics;
  27.     use diagnostics -verbose;
  28.  
  29.     enable  diagnostics;
  30.     disable diagnostics;
  31.  
  32. Using the C<splain> standalone filter program:
  33.  
  34.     perl program 2>diag.out
  35.     splain [-v] [-p] diag.out
  36.  
  37. =head1 DESCRIPTION
  38.  
  39. =head2 The C<diagnostics> Pragma
  40.  
  41. This module extends the terse diagnostics normally emitted by both the
  42. perl compiler and the perl interpreter, augmenting them with the more
  43. explicative and endearing descriptions found in L<perldiag>.  Like the
  44. other pragmata, it affects the compilation phase of your program rather
  45. than merely the execution phase.
  46.  
  47. To use in your program as a pragma, merely invoke
  48.  
  49.     use diagnostics;
  50.  
  51. at the start (or near the start) of your program.  (Note 
  52. that this I<does> enable perl's B<-w> flag.)  Your whole
  53. compilation will then be subject(ed :-) to the enhanced diagnostics.
  54. These still go out B<STDERR>.
  55.  
  56. Due to the interaction between runtime and compiletime issues,
  57. and because it's probably not a very good idea anyway,
  58. you may not use C<no diagnostics> to turn them off at compiletime.
  59. However, you may control their behaviour at runtime using the 
  60. disable() and enable() methods to turn them off and on respectively.
  61.  
  62. The B<-verbose> flag first prints out the L<perldiag> introduction before
  63. any other diagnostics.  The $diagnostics::PRETTY variable can generate nicer
  64. escape sequences for pagers.
  65.  
  66. Warnings dispatched from perl itself (or more accurately, those that match
  67. descriptions found in L<perldiag>) are only displayed once (no duplicate
  68. descriptions).  User code generated warnings a la warn() are unaffected,
  69. allowing duplicate user messages to be displayed.
  70.  
  71. =head2 The I<splain> Program
  72.  
  73. While apparently a whole nuther program, I<splain> is actually nothing
  74. more than a link to the (executable) F<diagnostics.pm> module, as well as
  75. a link to the F<diagnostics.pod> documentation.  The B<-v> flag is like
  76. the C<use diagnostics -verbose> directive.
  77. The B<-p> flag is like the
  78. $diagnostics::PRETTY variable.  Since you're post-processing with 
  79. I<splain>, there's no sense in being able to enable() or disable() processing.
  80.  
  81. Output from I<splain> is directed to B<STDOUT>, unlike the pragma.
  82.  
  83. =head1 EXAMPLES
  84.  
  85. The following file is certain to trigger a few errors at both
  86. runtime and compiletime:
  87.  
  88.     use diagnostics;
  89.     print NOWHERE "nothing\n";
  90.     print STDERR "\n\tThis message should be unadorned.\n";
  91.     warn "\tThis is a user warning";
  92.     print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
  93.     my $a, $b = scalar <STDIN>;
  94.     print "\n";
  95.     print $x/$y;
  96.  
  97. If you prefer to run your program first and look at its problem
  98. afterwards, do this:
  99.  
  100.     perl -w test.pl 2>test.out
  101.     ./splain < test.out
  102.  
  103. Note that this is not in general possible in shells of more dubious heritage, 
  104. as the theoretical 
  105.  
  106.     (perl -w test.pl >/dev/tty) >& test.out
  107.     ./splain < test.out
  108.  
  109. Because you just moved the existing B<stdout> to somewhere else.
  110.  
  111. If you don't want to modify your source code, but still have on-the-fly
  112. warnings, do this:
  113.  
  114.     exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&- 
  115.  
  116. Nifty, eh?
  117.  
  118. If you want to control warnings on the fly, do something like this.
  119. Make sure you do the C<use> first, or you won't be able to get
  120. at the enable() or disable() methods.
  121.  
  122.     use diagnostics; # checks entire compilation phase 
  123.     print "\ntime for 1st bogus diags: SQUAWKINGS\n";
  124.     print BOGUS1 'nada';
  125.     print "done with 1st bogus\n";
  126.  
  127.     disable diagnostics; # only turns off runtime warnings
  128.     print "\ntime for 2nd bogus: (squelched)\n";
  129.     print BOGUS2 'nada';
  130.     print "done with 2nd bogus\n";
  131.  
  132.     enable diagnostics; # turns back on runtime warnings
  133.     print "\ntime for 3rd bogus: SQUAWKINGS\n";
  134.     print BOGUS3 'nada';
  135.     print "done with 3rd bogus\n";
  136.  
  137.     disable diagnostics;
  138.     print "\ntime for 4th bogus: (squelched)\n";
  139.     print BOGUS4 'nada';
  140.     print "done with 4th bogus\n";
  141.  
  142. =head1 INTERNALS
  143.  
  144. Diagnostic messages derive from the F<perldiag.pod> file when available at
  145. runtime.  Otherwise, they may be embedded in the file itself when the
  146. splain package is built.   See the F<Makefile> for details.
  147.  
  148. If an extant $SIG{__WARN__} handler is discovered, it will continue
  149. to be honored, but only after the diagnostics::splainthis() function 
  150. (the module's $SIG{__WARN__} interceptor) has had its way with your
  151. warnings.
  152.  
  153. There is a $diagnostics::DEBUG variable you may set if you're desperately
  154. curious what sorts of things are being intercepted.
  155.  
  156.     BEGIN { $diagnostics::DEBUG = 1 } 
  157.  
  158.  
  159. =head1 BUGS
  160.  
  161. Not being able to say "no diagnostics" is annoying, but may not be
  162. insurmountable.
  163.  
  164. The C<-pretty> directive is called too late to affect matters.
  165. You have to do this instead, and I<before> you load the module.
  166.  
  167.     BEGIN { $diagnostics::PRETTY = 1 } 
  168.  
  169. I could start up faster by delaying compilation until it should be
  170. needed, but this gets a "panic: top_level" when using the pragma form
  171. in Perl 5.001e.
  172.  
  173. While it's true that this documentation is somewhat subserious, if you use
  174. a program named I<splain>, you should expect a bit of whimsy.
  175.  
  176. =head1 AUTHOR
  177.  
  178. Tom Christiansen <F<tchrist@mox.perl.com>>, 25 June 1995.
  179.  
  180. =cut
  181.  
  182. use strict;
  183. use 5.006;
  184. use Carp;
  185.  
  186. our $VERSION = 1.12;
  187. our $DEBUG;
  188. our $VERBOSE;
  189. our $PRETTY;
  190.  
  191. use Config;
  192. my($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
  193. if ($^O eq 'VMS') {
  194.     require VMS::Filespec;
  195.     $privlib = VMS::Filespec::unixify($privlib);
  196.     $archlib = VMS::Filespec::unixify($archlib);
  197. }
  198. my @trypod = (
  199.        "$archlib/pod/perldiag.pod",
  200.        "$privlib/pod/perldiag-$Config{version}.pod",
  201.        "$privlib/pod/perldiag.pod",
  202.        "$archlib/pods/perldiag.pod",
  203.        "$privlib/pods/perldiag-$Config{version}.pod",
  204.        "$privlib/pods/perldiag.pod",
  205.       );
  206. # handy for development testing of new warnings etc
  207. unshift @trypod, "./pod/perldiag.pod" if -e "pod/perldiag.pod";
  208. (my $PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];
  209.  
  210. if ($^O eq 'MacOS') {
  211.     # just updir one from each lib dir, we'll find it ...
  212.     ($PODFILE) = grep { -e } map { "$_:pod:perldiag.pod" } @INC;
  213. }
  214.  
  215.  
  216. $DEBUG ||= 0;
  217. my $WHOAMI = ref bless [];  # nobody's business, prolly not even mine
  218.  
  219. local $| = 1;
  220. local $_;
  221.  
  222. my $standalone;
  223. my(%HTML_2_Troff, %HTML_2_Latin_1, %HTML_2_ASCII_7);
  224.  
  225. CONFIG: {
  226.     our $opt_p = our $opt_d = our $opt_v = our $opt_f = '';
  227.  
  228.     unless (caller) {
  229.     $standalone++;
  230.     require Getopt::Std;
  231.     Getopt::Std::getopts('pdvf:')
  232.         or die "Usage: $0 [-v] [-p] [-f splainpod]";
  233.     $PODFILE = $opt_f if $opt_f;
  234.     $DEBUG = 2 if $opt_d;
  235.     $VERBOSE = $opt_v;
  236.     $PRETTY = $opt_p;
  237.     }
  238.  
  239.     if (open(POD_DIAG, $PODFILE)) {
  240.     warn "Happy happy podfile from real $PODFILE\n" if $DEBUG;
  241.     last CONFIG;
  242.     } 
  243.  
  244.     if (caller) {
  245.     INCPATH: {
  246.         for my $file ( (map { "$_/$WHOAMI.pm" } @INC), $0) {
  247.         warn "Checking $file\n" if $DEBUG;
  248.         if (open(POD_DIAG, $file)) {
  249.             while (<POD_DIAG>) {
  250.             next unless
  251.                 /^__END__\s*# wish diag dbase were more accessible/;
  252.             print STDERR "podfile is $file\n" if $DEBUG;
  253.             last INCPATH;
  254.             }
  255.         }
  256.         } 
  257.     }
  258.     } else { 
  259.     print STDERR "podfile is <DATA>\n" if $DEBUG;
  260.     *POD_DIAG = *main::DATA;
  261.     }
  262. }
  263. if (eof(POD_DIAG)) { 
  264.     die "couldn't find diagnostic data in $PODFILE @INC $0";
  265. }
  266.  
  267.  
  268. %HTML_2_Troff = (
  269.     'amp'    =>    '&',    #   ampersand
  270.     'lt'    =>    '<',    #   left chevron, less-than
  271.     'gt'    =>    '>',    #   right chevron, greater-than
  272.     'quot'    =>    '"',    #   double quote
  273.  
  274.     "Aacute"    =>    "A\\*'",    #   capital A, acute accent
  275.     # etc
  276.  
  277. );
  278.  
  279. %HTML_2_Latin_1 = (
  280.     'amp'    =>    '&',    #   ampersand
  281.     'lt'    =>    '<',    #   left chevron, less-than
  282.     'gt'    =>    '>',    #   right chevron, greater-than
  283.     'quot'    =>    '"',    #   double quote
  284.  
  285.     "Aacute"    =>    "\xC1"    #   capital A, acute accent
  286.  
  287.     # etc
  288. );
  289.  
  290. %HTML_2_ASCII_7 = (
  291.     'amp'    =>    '&',    #   ampersand
  292.     'lt'    =>    '<',    #   left chevron, less-than
  293.     'gt'    =>    '>',    #   right chevron, greater-than
  294.     'quot'    =>    '"',    #   double quote
  295.  
  296.     "Aacute"    =>    "A"    #   capital A, acute accent
  297.     # etc
  298. );
  299.  
  300. our %HTML_Escapes;
  301. *HTML_Escapes = do {
  302.     if ($standalone) {
  303.     $PRETTY ? \%HTML_2_Latin_1 : \%HTML_2_ASCII_7; 
  304.     } else {
  305.     \%HTML_2_Latin_1; 
  306.     }
  307. }; 
  308.  
  309. *THITHER = $standalone ? *STDOUT : *STDERR;
  310.  
  311. my %transfmt = (); 
  312. my $transmo = <<EOFUNC;
  313. sub transmo {
  314.     #local \$^W = 0;  # recursive warnings we do NOT need!
  315.     study;
  316. EOFUNC
  317.  
  318. my %msg;
  319. {
  320.     print STDERR "FINISHING COMPILATION for $_\n" if $DEBUG;
  321.     local $/ = '';
  322.     local $_;
  323.     my $header;
  324.     my $for_item;
  325.     while (<POD_DIAG>) {
  326.  
  327.     unescape();
  328.     if ($PRETTY) {
  329.         sub noop   { return $_[0] }  # spensive for a noop
  330.         sub bold   { my $str =$_[0];  $str =~ s/(.)/$1\b$1/g; return $str; } 
  331.         sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g;  return $str; } 
  332.         s/[BC]<(.*?)>/bold($1)/ges;
  333.         s/[LIF]<(.*?)>/italic($1)/ges;
  334.     } else {
  335.         s/[BC]<(.*?)>/$1/gs;
  336.         s/[LIF]<(.*?)>/$1/gs;
  337.     } 
  338.     unless (/^=/) {
  339.         if (defined $header) { 
  340.         if ( $header eq 'DESCRIPTION' && 
  341.             (   /Optional warnings are enabled/ 
  342.              || /Some of these messages are generic./
  343.             ) )
  344.         {
  345.             next;
  346.         }
  347.         s/^/    /gm;
  348.         $msg{$header} .= $_;
  349.          undef $for_item;    
  350.         }
  351.         next;
  352.     } 
  353.     unless ( s/=item (.*?)\s*\z//) {
  354.  
  355.         if ( s/=head1\sDESCRIPTION//) {
  356.         $msg{$header = 'DESCRIPTION'} = '';
  357.         undef $for_item;
  358.         }
  359.         elsif( s/^=for\s+diagnostics\s*\n(.*?)\s*\z// ) {
  360.         $for_item = $1;
  361.         } 
  362.         next;
  363.     }
  364.  
  365.     if( $for_item ) { $header = $for_item; undef $for_item } 
  366.     else {
  367.         $header = $1;
  368.         while( $header =~ /[;,]\z/ ) {
  369.         <POD_DIAG> =~ /^\s*(.*?)\s*\z/;
  370.         $header .= ' '.$1;
  371.         }
  372.     }
  373.  
  374.     # strip formatting directives from =item line
  375.     $header =~ s/[A-Z]<(.*?)>/$1/g;
  376.  
  377.         my @toks = split( /(%l?[dx]|%c|%(?:\.\d+)?s)/, $header );
  378.     if (@toks > 1) {
  379.             my $conlen = 0;
  380.             for my $i (0..$#toks){
  381.                 if( $i % 2 ){
  382.                     if(      $toks[$i] eq '%c' ){
  383.                         $toks[$i] = '.';
  384.                     } elsif( $toks[$i] eq '%d' ){
  385.                         $toks[$i] = '\d+';
  386.                     } elsif( $toks[$i] eq '%s' ){
  387.                         $toks[$i] = $i == $#toks ? '.*' : '.*?';
  388.                     } elsif( $toks[$i] =~ '%.(\d+)s' ){
  389.                         $toks[$i] = ".{$1}";
  390.                      } elsif( $toks[$i] =~ '^%l*x$' ){
  391.                         $toks[$i] = '[\da-f]+';
  392.                    }
  393.                 } elsif( length( $toks[$i] ) ){
  394.                     $toks[$i] =~ s/^.*$/\Q$&\E/;
  395.                     $conlen += length( $toks[$i] );
  396.                 }
  397.             }  
  398.             my $lhs = join( '', @toks );
  399.         $transfmt{$header}{pat} =
  400.               "    s{^$lhs}\n     {\Q$header\E}s\n\t&& return 1;\n";
  401.             $transfmt{$header}{len} = $conlen;
  402.     } else {
  403.             $transfmt{$header}{pat} =
  404.           "    m{^\Q$header\E} && return 1;\n";
  405.             $transfmt{$header}{len} = length( $header );
  406.     } 
  407.  
  408.     print STDERR "$WHOAMI: Duplicate entry: \"$header\"\n"
  409.         if $msg{$header};
  410.  
  411.     $msg{$header} = '';
  412.     } 
  413.  
  414.  
  415.     close POD_DIAG unless *main::DATA eq *POD_DIAG;
  416.  
  417.     die "No diagnostics?" unless %msg;
  418.  
  419.     # Apply patterns in order of decreasing sum of lengths of fixed parts
  420.     # Seems the best way of hitting the right one.
  421.     for my $hdr ( sort { $transfmt{$b}{len} <=> $transfmt{$a}{len} }
  422.                   keys %transfmt ){
  423.         $transmo .= $transfmt{$hdr}{pat};
  424.     }
  425.     $transmo .= "    return 0;\n}\n";
  426.     print STDERR $transmo if $DEBUG;
  427.     eval $transmo;
  428.     die $@ if $@;
  429. }
  430.  
  431. if ($standalone) {
  432.     if (!@ARGV and -t STDIN) { print STDERR "$0: Reading from STDIN\n" } 
  433.     while (defined (my $error = <>)) {
  434.     splainthis($error) || print THITHER $error;
  435.     } 
  436.     exit;
  437.  
  438. my $olddie;
  439. my $oldwarn;
  440.  
  441. sub import {
  442.     shift;
  443.     $^W = 1; # yup, clobbered the global variable; 
  444.          # tough, if you want diags, you want diags.
  445.     return if defined $SIG{__WARN__} && ($SIG{__WARN__} eq \&warn_trap);
  446.  
  447.     for (@_) {
  448.  
  449.     /^-d(ebug)?$/            && do {
  450.                     $DEBUG++;
  451.                     next;
  452.                    };
  453.  
  454.     /^-v(erbose)?$/     && do {
  455.                     $VERBOSE++;
  456.                     next;
  457.                    };
  458.  
  459.     /^-p(retty)?$/         && do {
  460.                     print STDERR "$0: I'm afraid it's too late for prettiness.\n";
  461.                     $PRETTY++;
  462.                     next;
  463.                    };
  464.  
  465.     warn "Unknown flag: $_";
  466.     } 
  467.  
  468.     $oldwarn = $SIG{__WARN__};
  469.     $olddie = $SIG{__DIE__};
  470.     $SIG{__WARN__} = \&warn_trap;
  471.     $SIG{__DIE__} = \&death_trap;
  472.  
  473. sub enable { &import }
  474.  
  475. sub disable {
  476.     shift;
  477.     return unless $SIG{__WARN__} eq \&warn_trap;
  478.     $SIG{__WARN__} = $oldwarn || '';
  479.     $SIG{__DIE__} = $olddie || '';
  480.  
  481. sub warn_trap {
  482.     my $warning = $_[0];
  483.     if (caller eq $WHOAMI or !splainthis($warning)) {
  484.     print STDERR $warning;
  485.     } 
  486.     &$oldwarn if defined $oldwarn and $oldwarn and $oldwarn ne \&warn_trap;
  487. };
  488.  
  489. sub death_trap {
  490.     my $exception = $_[0];
  491.  
  492.     # See if we are coming from anywhere within an eval. If so we don't
  493.     # want to explain the exception because it's going to get caught.
  494.     my $in_eval = 0;
  495.     my $i = 0;
  496.     while (1) {
  497.       my $caller = (caller($i++))[3] or last;
  498.       if ($caller eq '(eval)') {
  499.     $in_eval = 1;
  500.     last;
  501.       }
  502.     }
  503.  
  504.     splainthis($exception) unless $in_eval;
  505.     if (caller eq $WHOAMI) { print STDERR "INTERNAL EXCEPTION: $exception"; } 
  506.     &$olddie if defined $olddie and $olddie and $olddie ne \&death_trap;
  507.  
  508.     return if $in_eval;
  509.  
  510.     # We don't want to unset these if we're coming from an eval because
  511.     # then we've turned off diagnostics.
  512.  
  513.     # Switch off our die/warn handlers so we don't wind up in our own
  514.     # traps.
  515.     $SIG{__DIE__} = $SIG{__WARN__} = '';
  516.  
  517.     # Have carp skip over death_trap() when showing the stack trace.
  518.     local($Carp::CarpLevel) = 1;
  519.  
  520.     confess "Uncaught exception from user code:\n\t$exception";
  521.     # up we go; where we stop, nobody knows, but i think we die now
  522.     # but i'm deeply afraid of the &$olddie guy reraising and us getting
  523.     # into an indirect recursion loop
  524. };
  525.  
  526. my %exact_duplicate;
  527. my %old_diag;
  528. my $count;
  529. my $wantspace;
  530. sub splainthis {
  531.     local $_ = shift;
  532.     local $\;
  533.     ### &finish_compilation unless %msg;
  534.     s/\.?\n+$//;
  535.     my $orig = $_;
  536.     # return unless defined;
  537.  
  538.     # get rid of the where-are-we-in-input part
  539.     s/, <.*?> (?:line|chunk).*$//;
  540.  
  541.     # Discard 1st " at <file> line <no>" and all text beyond
  542.     # but be aware of messsages containing " at this-or-that"
  543.     my $real = 0;
  544.     my @secs = split( / at / );
  545.     $_ = $secs[0];
  546.     for my $i ( 1..$#secs ){
  547.         if( $secs[$i] =~ /.+? (?:line|chunk) \d+/ ){
  548.             $real = 1;
  549.             last;
  550.         } else {
  551.             $_ .= ' at ' . $secs[$i];
  552.     }
  553.     }
  554.     
  555.     # remove parenthesis occurring at the end of some messages 
  556.     s/^\((.*)\)$/$1/;
  557.  
  558.     if ($exact_duplicate{$orig}++) {
  559.     return &transmo;
  560.     } else {
  561.     return 0 unless &transmo;
  562.     }
  563.  
  564.     $orig = shorten($orig);
  565.     if ($old_diag{$_}) {
  566.     autodescribe();
  567.     print THITHER "$orig (#$old_diag{$_})\n";
  568.     $wantspace = 1;
  569.     } else {
  570.     autodescribe();
  571.     $old_diag{$_} = ++$count;
  572.     print THITHER "\n" if $wantspace;
  573.     $wantspace = 0;
  574.     print THITHER "$orig (#$old_diag{$_})\n";
  575.     if ($msg{$_}) {
  576.         print THITHER $msg{$_};
  577.     } else {
  578.         if (0 and $standalone) { 
  579.         print THITHER "    **** Error #$old_diag{$_} ",
  580.             ($real ? "is" : "appears to be"),
  581.             " an unknown diagnostic message.\n\n";
  582.         }
  583.         return 0;
  584.     } 
  585.     }
  586.     return 1;
  587.  
  588. sub autodescribe {
  589.     if ($VERBOSE and not $count) {
  590.     print THITHER &{$PRETTY ? \&bold : \&noop}("DESCRIPTION OF DIAGNOSTICS"),
  591.         "\n$msg{DESCRIPTION}\n";
  592.     } 
  593.  
  594. sub unescape { 
  595.     s {
  596.             E<  
  597.             ( [A-Za-z]+ )       
  598.             >   
  599.     } { 
  600.          do {   
  601.              exists $HTML_Escapes{$1}
  602.                 ? do { $HTML_Escapes{$1} }
  603.                 : do {
  604.                     warn "Unknown escape: E<$1> in $_";
  605.                     "E<$1>";
  606.                 } 
  607.          } 
  608.     }egx;
  609. }
  610.  
  611. sub shorten {
  612.     my $line = $_[0];
  613.     if (length($line) > 79 and index($line, "\n") == -1) {
  614.     my $space_place = rindex($line, ' ', 79);
  615.     if ($space_place != -1) {
  616.         substr($line, $space_place, 1) = "\n\t";
  617.     } 
  618.     } 
  619.     return $line;
  620.  
  621.  
  622. 1 unless $standalone;  # or it'll complain about itself
  623. __END__ # wish diag dbase were more accessible
  624.  
  625. __END__
  626. :endofperl
  627.